﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rapid
{
    public partial class frmTranslationChart : frmMenustrip
    {
        RapidDataDataContext db = new RapidDataDataContext();

        Observation drugLine;
        Observation drugNoLine;
        Observation drugUnclear;

        Observation monitoredLine;
        Observation monitoredNoLine;
        Observation monitoredUnclear;

        Observation adulterantsNormal;
        Observation adulterantsAbnormal;

        CheckBox[] _checkboxes;
        TextBox[] _textboxes;

        public frmTranslationChart()
        {
            InitializeComponent();

            #region style for button
            btnCancel.TabStop = false;
            btnCancel.FlatStyle = FlatStyle.Flat;
            btnCancel.FlatAppearance.BorderSize = 0;
            btnCancel.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent

            btnApply.TabStop = false;
            btnApply.FlatStyle = FlatStyle.Flat;
            btnApply.FlatAppearance.BorderSize = 0;
            btnApply.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent
            #endregion


            #region drug
            drugLine = db.Observations.Single(ob => ob.TypeDrug == 1 && ob.ObservationType == EObservation.Line.ToString());
            txt_DrugLine_Inter.Text = drugLine.Interpretation;
            rdo_Drugs_Line.Checked = (drugLine.Flag == 1) ? true : false;
            txt_Drugs_Line.Text = drugLine.Comments;
            if (rdo_Drugs_Line.Checked)
                HighLight(txt_DrugLine_Inter);

            drugNoLine = db.Observations.Single(ob => ob.TypeDrug == 1 && ob.ObservationType == EObservation.NoLine.ToString());
            txt_Drugs_NoLine_Inter.Text = drugNoLine.Interpretation;
            rdo_Drugs_NoLine.Checked = (drugNoLine.Flag == 1) ? true : false;
            txt_Drugs_NoLine.Text = drugNoLine.Comments;
            if (rdo_Drugs_NoLine.Checked)
                HighLight(txt_Drugs_NoLine_Inter);


            drugUnclear = db.Observations.Single(ob => ob.TypeDrug == 1 && ob.ObservationType == EObservation.Unclear.ToString());
            txt_Drugs_Unclear_Inter.Text = drugUnclear.Interpretation;
            rdo_Drugs_Unclear.Checked = (drugUnclear.Flag == 1) ? true : false;
            txt_Drugs_Unclear.Text = drugUnclear.Comments;
            if (rdo_Drugs_Unclear.Checked)
                HighLight(txt_Drugs_Unclear_Inter);

            #endregion

            #region Monitored
            monitoredLine = db.Observations.Single(ob => ob.TypeDrug == 2 && ob.ObservationType == EObservation.Line.ToString());
            txt_Monitored_Line_Inter.Text = monitoredLine.Interpretation;
            rdo_Monitored_Line.Checked = (monitoredLine.Flag == 1) ? true : false;
            txt_Monitored_Line.Text = monitoredLine.Comments;
            if (rdo_Monitored_Line.Checked)
                HighLight(txt_Monitored_Line_Inter);


            monitoredNoLine = db.Observations.Single(ob => ob.TypeDrug == 2 && ob.ObservationType == EObservation.NoLine.ToString());
            txt_Monitored_NoLine_Inter.Text = monitoredNoLine.Interpretation;
            rdo_Monitored_NoLine.Checked = (monitoredNoLine.Flag == 1) ? true : false;
            txt_Monitored_NoLine.Text = monitoredNoLine.Comments;
            if (rdo_Monitored_NoLine.Checked)
                HighLight(txt_Monitored_NoLine_Inter);


            monitoredUnclear = db.Observations.Single(ob => ob.TypeDrug == 2 && ob.ObservationType == EObservation.Unclear.ToString());
            txt_Monitored_Unclear_Inter.Text = monitoredUnclear.Interpretation;
            rdo_Monitored_Unclear.Checked = (monitoredUnclear.Flag == 1) ? true : false;
            txt_Monitored_Unclear.Text = monitoredUnclear.Comments;
            if (rdo_Monitored_Unclear.Checked)
                HighLight(txt_Monitored_Unclear_Inter);

            #endregion

            #region Adulterants
            adulterantsNormal = db.Observations.Single(ob => ob.TypeDrug == 3 && ob.ObservationType == EObservationCommon.Normal.ToString());
            txt_Adulterants_Normal_Inter.Text = adulterantsNormal.Interpretation;
            rdo_Adulterants_Normal.Checked = (adulterantsNormal.Flag == 1) ? true : false;
            txt_Adulterants_Normal.Text = adulterantsNormal.Comments;
            if (rdo_Adulterants_Normal.Checked)
                HighLight(txt_Adulterants_Normal_Inter);


            adulterantsAbnormal = db.Observations.Single(ob => ob.TypeDrug == 3 && ob.ObservationType == EObservationCommon.Abnormal.ToString());
            txt_Adulterants_Abnormal_Inter.Text = adulterantsAbnormal.Interpretation;
            rdo_Adulterants_Abnormal.Checked = (adulterantsAbnormal.Flag == 1) ? true : false;
            txt_Adulterants_Abnormal.Text = adulterantsAbnormal.Comments;
            if (rdo_Adulterants_Abnormal.Checked)
                HighLight(txt_Adulterants_Abnormal_Inter);

            #endregion

            _checkboxes = new CheckBox[] { 
                            rdo_Drugs_Line, rdo_Drugs_NoLine, rdo_Drugs_Unclear, 
                            rdo_Monitored_Line, rdo_Monitored_NoLine, rdo_Monitored_Unclear,
                            rdo_Adulterants_Normal, rdo_Adulterants_Abnormal
                        };

            _textboxes = new TextBox[] { 
                            txt_DrugLine_Inter, txt_Drugs_NoLine_Inter, txt_Drugs_Unclear_Inter,
                            txt_Monitored_Line_Inter, txt_Monitored_NoLine_Inter, txt_Monitored_Unclear_Inter,
                            txt_Adulterants_Normal_Inter, txt_Adulterants_Abnormal_Inter
            };


            foreach (var checkBox in _checkboxes)
                checkBox.CheckedChanged += new EventHandler(ShowCheckedCheckboxes);
        }

        void ShowCheckedCheckboxes(object sender, EventArgs e)
        {
            CheckBox checkbox = sender as CheckBox;

            int index = Array.IndexOf(_checkboxes, checkbox);

            if(checkbox.Checked)
                _textboxes[index].ForeColor = Color.Red;
            else
                _textboxes[index].ForeColor = Color.Black;
        }

        private void frmTranslationChart_Load(object sender, EventArgs e)
        {
            //this.TopMost = true;
            //this.FormBorderStyle = FormBorderStyle.None;
            //this.WindowState = FormWindowState.Maximized;
        }

        private void radioButton8_Click(object sender, EventArgs e)
        {

        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            //db.Observations.Single(ob => ob.TypeDrug == 1 && ob.ObservationType == EObservation.Line.ToString()).Comments
            if (string.IsNullOrEmpty(txtAuthorrized.Text))
            {
                DialogResult result = MessageBox.Show("Skip authorization?", "Alert", MessageBoxButtons.YesNo);
                if (result == DialogResult.No)
                    return;
            }
                //AddAuthor();
                UpdateTranslation();

                this.Hide();
                //frmStart _frmStart = new frmStart();
                //_frmStart.Show();
                frmRapidRead _frmRapidRead = new frmRapidRead();
                _frmRapidRead.Show();
        }

        public void HighLight(TextBox txtHighLight)
        {
            txtHighLight.ForeColor = Color.Red;
        }

        public void AddAuthor()
        {
            TranslationAuthor author = new TranslationAuthor { Name = txtAuthorrized.Text, DateModified = DateTime.Now };
            db.TranslationAuthors.InsertOnSubmit(author);
            db.SubmitChanges();
        }

        public void Reload()
        {
            this.Hide();
            frmTranslationChart _frmTranslationChart = new frmTranslationChart();
            _frmTranslationChart.Show();
        }

        public void UpdateTranslation()
        {
            #region drugs
            drugLine.Interpretation = txt_DrugLine_Inter.Text;
            drugLine.Flag = (rdo_Drugs_Line.Checked) ? (byte)1 : (byte)0;
            drugLine.Comments = txt_Drugs_Line.Text;

            drugNoLine.Interpretation = txt_Drugs_NoLine_Inter.Text;
            drugNoLine.Flag = (rdo_Drugs_NoLine.Checked) ? (byte)1 : (byte)0;
            drugNoLine.Comments = txt_Drugs_NoLine.Text;

            drugUnclear.Interpretation = txt_Drugs_Unclear_Inter.Text;
            drugUnclear.Flag = (rdo_Drugs_Unclear.Checked) ? (byte)1 : (byte)0;
            drugUnclear.Comments = txt_Drugs_Unclear.Text;
            #endregion

            #region monitored
            monitoredLine.Interpretation = txt_Monitored_Line_Inter.Text;
            monitoredLine.Flag = (rdo_Monitored_Line.Checked) ? (byte)1 : (byte)0;
            monitoredLine.Comments = txt_Monitored_Line.Text;

            monitoredNoLine.Interpretation = txt_Monitored_NoLine_Inter.Text;
            monitoredNoLine.Flag = (rdo_Monitored_NoLine.Checked) ? (byte)1 : (byte)0;
            monitoredNoLine.Comments = txt_Monitored_NoLine.Text;

            monitoredUnclear.Interpretation = txt_Monitored_Unclear_Inter.Text;
            monitoredUnclear.Flag = (rdo_Monitored_Unclear.Checked) ? (byte)1 : (byte)0;
            monitoredUnclear.Comments = txt_Monitored_Unclear.Text;
            #endregion

            #region adulterants
            adulterantsNormal.Interpretation = txt_Adulterants_Normal_Inter.Text;
            adulterantsNormal.Flag = (rdo_Adulterants_Normal.Checked) ? (byte)1 : (byte)0;
            adulterantsNormal.Comments = txt_Adulterants_Normal.Text;

            adulterantsAbnormal.Interpretation = txt_Adulterants_Abnormal_Inter.Text;
            adulterantsAbnormal.Flag = (rdo_Adulterants_Abnormal.Checked) ? (byte)1 : (byte)0;
            adulterantsAbnormal.Comments = txt_Adulterants_Abnormal.Text;
            #endregion

            //db.
            db.SubmitChanges();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure want to cancel?", "Question", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                this.Hide();
                frmRapidRead _frmRapidRead = new frmRapidRead();
                _frmRapidRead.Show();
            }            
        }
    }
}
